<?php if(!defined('DB_NAME')) die();
// 替换评论表情
if( !isset( $wpsmiliestrans ) ){
	$wpsmiliestrans = array(
		':01:'=>'01.gif', ':02:'=>'02.gif', ':03:'=>'03.gif', ':04:'=>'04.gif', ':05:'=>'05.gif', ':06:'=>'06.gif', ':07:'=>'07.gif', ':08:'=>'08.gif', ':09:'=>'09.gif', ':10:'=>'10.gif', ':11:'=>'11.gif', ':12:'=>'12.gif', ':13:'=>'13.gif', ':14:'=>'14.gif', ':15:'=>'15.gif', ':16:'=>'16.gif',
	);
}
function custom_smilies_src($src, $img){ return TPDIR.'/res/smiley/'.$img; }
add_filter('smilies_src', 'custom_smilies_src', 1, 10);

// 评论内容
function ini_comment($comment, $args, $depth){
	$GLOBALS['comment'] = $comment;
	global $commentcount,$wpdb,$post;
	$size = get('ini_avatar')? get('ini_avatar'): 50;
	$left = $size + 20;
	if(!$commentcount){
		$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");
		$cnt = count($comments);
		$page = get_query_var('cpage');
		$cpp = get('comments_per_page');
		if( ceil($cnt / $cpp) == 1 || ( $page > 1 && $page == ceil($cnt/$cpp) ) ){
			$commentcount = $cnt + 1;
		} else{
			$commentcount = $cpp * $page + 1;
		}
	}
?>
<li class="comment-content">
	<div id="comment-<?php comment_ID()?>" class="comment-body">
<?php if( !ismobile()&&get('ini_type')=='显示' || ismobile()&&get('ini_mava') ){?>
		<?php
if( isIE8() ) $fixie = "style=width:{$size}px;height:{$size}px ";
		echo "<img class=\"avatar\" width=$size height=$size src=\"".TPDIR."/res/avatar.jpg\" alt=\"avatar\" data-original=\"".preg_replace(array('/^.+(src=)(\"|\')/i', '/(\"|\')\sclass=(\"|\').+$/i'), array('', ''), get_avatar($comment, $size) )."\" $fixie/>"?>
<?php } ?>
		<div class="comment-text<?php if($comment->user_id == 1) echo " admin"?>">
<?php
if( !ismobile() ):
echo "<span class=\"floor\">";
if( get("ini_depth")=="显示" ){
	if(!$parent_id = $comment->comment_parent ){ --$commentcount; }
	echo '<span>#'.$commentcount.'</span>';
	$depth!==1 ? print '-'.--$depth : '';
}
else {
	if(!$parent_id = $comment->comment_parent){
		switch ($commentcount){
			default:printf('<span>#%1$s</span>',--$commentcount);
		}
	}
}
echo "</span>";
endif;
?>
			<strong><?php comment_author_link()?></strong><?php if( $comment->comment_parent && !ismobile() ) echo '<strong> @ <a href="#comment-'.$comment->comment_parent.'">'.get_comment($comment->comment_parent)-> comment_author.'</a></strong>'?>
<?php if( $comment->comment_approved=='0' ){?>
			<span style="color:#C00">您的评论正在等待审核中...</span><br>
<?php }?>
			<?php comment_text()?>
			<small><?php
			if( !ismobile() ){ comment_date('Y-m-d '); comment_time(); }
			comment_reply_link( array_merge( $args, array('reply_text' => '回复','depth' => $depth,'max_depth' => $args['max_depth']) ) );
			if( current_user_can('edit_post') ){
				$url = get_bloginfo('url');
				echo '<a id="delete-'. $comment->comment_ID .'" href="' . wp_nonce_url("$url/wp-admin/comment.php?action=deletecomment&p=" . $comment->comment_post_ID . '&c=' . $comment->comment_ID,'delete-comment_' . $comment->comment_ID) . '"" >删除</a>';
				edit_comment_link('编辑');
			}
?></small>
		</div>
	</div>
<?php
}
function ini_end_comment(){ echo "</li>\n"; }

// 屏蔽全英文评论
function ini_refused_spam( $comment_data ){
	$pattern = '/[一-龥]/u';
	if( !preg_match($pattern, $comment_data['comment_content']) ){
		err( "You should type some Chinese word (like \"你好\") in your comment to pass the spam-check, thanks for your patience!" );
	}
	return( $comment_data );
}
add_filter('preprocess_comment','ini_refused_spam');

?>